package com.example.swaggerDemo.service;

import com.example.swaggerDemo.Dao.articleUserDao;
import com.example.swaggerDemo.entity.ArticleEntity;
import com.example.swaggerDemo.entity.articleUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Repository
public class articleUserService implements articleUserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

    @Override
    public int insert(@RequestParam("userid") String userid, @RequestParam("upid") String upid) throws SQLException {
        String createtime= df.format(new Date());
        String sql = "insert into articleuser (userid,upid,createtime) " +
                "values(?,?,?)";
        Object args[] = {userid,upid,createtime};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public articleUserEntity findid(@RequestParam("userid") String userid, @RequestParam("upid") String upid) throws SQLException {
        String sql = "select * from articleuser WHERE  userid= '" +  userid + "' AND upid="+"'"+upid+"'" ;
        articleUserEntity dto=null;

        try { dto= jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<articleUserEntity>(articleUserEntity.class));
        }
        catch(Exception ex){

        }
        return dto;
    }

}
